package com.bsoft.gol.editor.service.impl;

import ctd.util.BeanUtils;
import ctd.util.JSONUtils;
import ctd.util.annotation.RpcService;
import ctd.util.exception.CodedBaseRuntimeException;
import com.bsoft.gol.editor.dao.ControlReportEntityDAO;
import hcn.util.BaseResponse;
import com.bsoft.gol.editor.editor.request.HospitalExamReportListRequest;
import com.bsoft.gol.editor.editor.request.HospitalLabReportDetailRequest;
import com.bsoft.gol.editor.editor.request.HospitalLabReportListRequest;
import com.bsoft.gol.editor.editor.request.QueryCrucialIndexRequest;
import com.bsoft.gol.editor.editor.response.HospitalExamReportDetailResponse;
import com.bsoft.gol.editor.editor.response.HospitalExamReportListResponse;
import com.bsoft.gol.editor.editor.response.HospitalLabReportDetailResponse;
import com.bsoft.gol.editor.editor.response.HospitalLabReportListResponse;
import hcn.base.his.vo.*;
import hcn.rep.request.ListQueryHisReq;
import org.atag.dao.model.SimplePageInfo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.bsoft.gol.editor.service.EditorHisService;
import com.bsoft.gol.editor.service.rpc.ICrucialIndexService;
import service.rpc.IPatientRecordQueryService;
import com.bsoft.gol.editor.utils.HttpClientUtils;

import java.io.IOException;
import java.util.List;


@Service("editorHisService")
public class EditorHisServiceImpl implements EditorHisService {
    private static Logger logger = LoggerFactory.getLogger(EditorHisServiceImpl.class);
    @Autowired
    private ControlReportEntityDAO controlReportEntityDAO;

    @Autowired
    IPatientRecordQueryService patientRecordQueryService;

    @Autowired
    ICrucialIndexService crucialIndexService;

    @Value("${indexTree_url}")
    private String indexTreeUrl;

    /**
     * 此接口接收的OrganizationId  为localOrgId
     *
     * @param request
     * @return
     */
    @Override
    public BaseResponse<HospitalExamReportListResponse> queryHospitalExamReportList(HospitalExamReportListRequest request) {

//        if (StringUtils.isNotEmpty(request.getHcnPatientIdentityCardType())) {
//            request.setCardType(IdentityTransformUtil.hcnIdentityToHuLian(request.getHcnPatientIdentityCardType()));
//        }
//        String url = OrgIdResource.getDemoUrl(request.getOrganizationId());
        logger.info("检查报告列表请求入参=" + JSONUtils.writeValueAsString(request));
//        request.setHisUrl("http://10.8.0.87:9090");
        try {

//                String result = HttpClientUtils.doPost(request.getHisUrl() + "/checkreport/listCheckReport", JSONUtils.writeValueAsString(request));
            String result = HttpClientUtils.doPost(indexTreeUrl + "/api/auth/patient/queryInspectionReportRecords", JSONUtils.writeValueAsString(request));

//                String result = HttpClientUtils.doPost("http://10.8.0.87:8080/platform/checkreport/listCheckReport", JSONUtils.writeValueAsString(request));

            if (StringUtils.isNotEmpty(result)) {
                BaseResponse<HospitalExamReportListResponse> baseResponse = JSONUtils.parse(result, new BaseResponse<HospitalExamReportListResponse>().getClass());
                baseResponse.getData();
                HospitalExamReportDetailResponse hospitalExamReportDetailResponse = new HospitalExamReportDetailResponse();

//                    if (null != baseResponse && null != baseResponse.getData()) {
//                        HospitalExamReportListResponse hospitalExamReportListResponse = new HospitalExamReportListResponse();
//                        BeanUtils.copy(baseResponse.getData(), hospitalExamReportListResponse);
//
//                        return hospitalExamReportListResponse;
//                    }
                return baseResponse;
            } else {
                throw new CodedBaseRuntimeException(201, "查询检查报告列表失败！");
            }
        } catch (IOException e) {
            e.printStackTrace();
            throw new CodedBaseRuntimeException(201, "查询检查报告列表失败！", e);
        }


    }

    /**
     * 此接口接收的OrganizationId  为localOrgId
     *
     * @param request
     * @return
     */
    @Override
    @RpcService
    public BaseResponse<HospitalExamReportDetailResponse> getExamReportDetail(HospitalExamReportListRequest request) {
//        String url = OrgIdResource.getDemoUrl(request.getOrganizationId());
        logger.info("检查报告详情请求入参=" + JSONUtils.writeValueAsString(request));
        request.setHisUrl("http://10.8.0.87:9090");
        if (StringUtils.isNotEmpty(request.getHisUrl())) {
            try {
                String result = HttpClientUtils.doPost(request.getHisUrl() + "/checkreport/listCheckReport", JSONUtils.writeValueAsString(request));
//                String result = HttpClientUtils.doPost("http://10.8.0.87:8080/platform/checkreport/listCheckReport",JSONUtils.writeValueAsString(request));
                if (StringUtils.isNotEmpty(result)) {
                    BaseResponse<HospitalExamReportDetailResponse> baseResponse = JSONUtils.parse(result, new BaseResponse<HospitalExamReportDetailResponse>().getClass());
                    if (null != baseResponse) {
                        if (null != baseResponse) {
                            if (null != baseResponse.getData()) {
                                HospitalExamReportDetailResponse hospitalExamReportDetailResponse = new HospitalExamReportDetailResponse();
                                BeanUtils.copy(baseResponse.getData(), hospitalExamReportDetailResponse);

                                if (null != hospitalExamReportDetailResponse) {
                                    List<HospitalExamReportDetailResponse.ListBean> list = hospitalExamReportDetailResponse.getList();

//                                    if(null!=list&&list.size()>0){
//                                        for(HospitalExamReportDetailResponse.ListBean listBean:list ){
//                                            List<HospitalExamReportDetailResponse.ListBean.ReportContentsBean> reportContentsBeanList=listBean.getReportContents();
//                                            for(HospitalExamReportDetailResponse.ListBean.ReportContentsBean reportContentsBean:reportContentsBeanList){
//                                                ControlReportEntity controlReportEntity=new ControlReportEntity();
//                                                controlReportEntity.setReportId(UUID.randomUUID().toString().replaceAll("-", ""));
//                                                reportContentsBean.setReportId(controlReportEntity.getReportId());
//                                                controlReportEntity.setSubassemblyType("1");
//                                                controlReportEntity.setItemContent(reportContentsBean.getItemContent());
//                                                controlReportEntity.setItemName(reportContentsBean.getItemName());
//                                                controlReportEntity.setItemId(reportContentsBean.getItemId());
//                                                controlReportEntityDAO.save(controlReportEntity);
//
//                                            }
//                                        }
//
////                                        if(null!=listBean){
////                                            String localOrgId = PropUtil.getHcnOrgId(request.getOrganizationId(),PropUtil.HIS);
////
////                                            if(StringUtils.isNotEmpty(localOrgId)){
////                                                listBean.setOrganizationId(localOrgId);
////                                            }
////                                        }
//
//                                    }
                                }
                            }
                        }
                    }
                    return baseResponse;
                } else {
                    throw new CodedBaseRuntimeException(201, "查询检查报告详情失败！");
                }
            } catch (IOException e) {
                e.printStackTrace();
                throw new CodedBaseRuntimeException(201, "查询检查报告详情失败！", e);
            }

        }
        return null;
    }


    @Override
    @RpcService
    public BaseResponse<HospitalLabReportListResponse> queryHospitalLabReportList(HospitalLabReportListRequest request) {
//        String url = OrgIdResource.getDemoUrl(request.getOrganizationId());
//        if(StringUtils.isNotEmpty(request.getHcnPatientIdentityCardType())){
//            request.setCardType(IdentityTransformUtil.hcnIdentityToHuLian(request.getHcnPatientIdentityCardType()));
//        }
        logger.info("检验报告列表请求入参=" + JSONUtils.writeValueAsString(request));
        request.setHisUrl("http://10.8.3.125:8067");
        if (StringUtils.isNotEmpty(request.getHisUrl())) {
            try {
                String result = HttpClientUtils.doPost(request.getHisUrl() + "/LisInterface/inspectionreport/listInspectionReport", JSONUtils.writeValueAsString(request));
//                String result = HttpClientUtils.doPost("http://10.8.3.125:8067/LisInterface/inspectionreport/listInspectionReport",JSONUtils.writeValueAsString(request));
                if (StringUtils.isNotEmpty(result)) {
                    BaseResponse<HospitalLabReportListResponse> baseResponse = JSONUtils.parse(result, new BaseResponse<HospitalLabReportListResponse>().getClass());
                    return baseResponse;
//                    if(null!=baseResponse&&null!=baseResponse.getData()){
//                        HospitalLabReportListResponse labReportListResponse = new HospitalLabReportListResponse();
//                        BeanUtils.copy(baseResponse.getData(),labReportListResponse);
//
//                        return labReportListResponse;
//                    }
                } else {
                    throw new CodedBaseRuntimeException(201, "查询检验报告列表失败！");
                }
            } catch (IOException e) {
                e.printStackTrace();
                throw new CodedBaseRuntimeException(201, "查询检验报告列表失败！", e);
            }

        }
        return null;
    }

    @Override
    @RpcService
    public BaseResponse<HospitalLabReportDetailResponse> getLabReportDetail(HospitalLabReportDetailRequest request) {
//        String url = OrgIdResource.getDemoUrl(request.getOrganizationId());
        logger.info("检验报告详情请求入参=" + JSONUtils.writeValueAsString(request));
        request.setHisUrl("http://10.8.3.125:8067");
        if (StringUtils.isNotEmpty(request.getHisUrl())) {
            try {
                String result = HttpClientUtils.doPost(request.getHisUrl() + "/LisInterface/inspectionreport/listInspectionReport", JSONUtils.writeValueAsString(request));
//                String result = HttpClientUtils.doPost("http://10.8.3.125:8067/LisInterface/inspectionreport/listInspectionReport",JSONUtils.writeValueAsString(request));
                if (StringUtils.isNotEmpty(result)) {
                    BaseResponse<HospitalLabReportDetailResponse> baseResponse = JSONUtils.parse(result, new BaseResponse<HospitalLabReportDetailResponse>().getClass());
                    return baseResponse;
//                    if(null!=baseResponse){
//                        if(null!=baseResponse){
//                            if(null!=baseResponse.getData()){
//                                HospitalLabReportDetailResponse hospitalLabReportDetailResponse = new HospitalLabReportDetailResponse();
//                                BeanUtils.copy(baseResponse.getData(),hospitalLabReportDetailResponse);
//
//                                if(null!=hospitalLabReportDetailResponse){
//                                    List<HospitalLabReportDetailResponse.ListBean> list = hospitalLabReportDetailResponse.getList();
//
//                                    if(null!=list&&list.size()>0){
//                                        HospitalLabReportDetailResponse.ListBean listBean= list.get(0);
//                                        return listBean;
//                                    }
//                                }
//                            }
//                        }
//                    }
                } else {
                    throw new CodedBaseRuntimeException(201, "查询检验报告详情失败！");
                }
            } catch (IOException e) {
                e.printStackTrace();
                throw new CodedBaseRuntimeException(201, "查询检验报告详情失败！", e);
            }

        }
        return null;
    }

    @Override
    @RpcService
    public BaseResponse queryIndexTree(QueryCrucialIndexRequest request) {
        logger.info("查询指标请求入参=" + JSONUtils.writeValueAsString(request));
        BaseResponse baseResponse = new BaseResponse<>();
        try {
            CrucialIndexTotalResponse crucialIndexTotalResponse = crucialIndexService.queryCrucialIndex(request);
            baseResponse.setData(crucialIndexTotalResponse);
        } catch (Exception e) {
            throw new CodedBaseRuntimeException(201, "查询指标列表失败！");
        }
        return baseResponse;
    }


    @Override
    public BaseResponse queryOutpatientRecords(ListQueryHisReq request) {
        logger.info("门诊列表请求入参=" + JSONUtils.writeValueAsString(request));
        BaseResponse baseResponse = new BaseResponse<>();
        try {
            SimplePageInfo<OutpatientRecordListVo> pageInfo = patientRecordQueryService.queryOutpatientRecords(request);
            baseResponse.setData(pageInfo);
        } catch (Exception e) {
            throw new CodedBaseRuntimeException(201, "查询门诊列表失败！");
        }
        return baseResponse;
    }

    @Override
    public BaseResponse queryHospitalRecords(ListQueryHisReq request) {
        logger.info("住院列表请求入参=" + JSONUtils.writeValueAsString(request));
        BaseResponse baseResponse = new BaseResponse<>();
        try {
            SimplePageInfo<InpatientVisitRecordListVo> pageInfo = patientRecordQueryService.queryHospitalRecords(request);
            baseResponse.setData(pageInfo);
        } catch (Exception e) {
            throw new CodedBaseRuntimeException(201, "查询住院列表失败！");
        }
        return baseResponse;
    }

    @Override
    public BaseResponse queryInspectionReportRecords(ListQueryHisReq request) {
        logger.info("检验、检查、治疗记录请求入参=" + JSONUtils.writeValueAsString(request));
        BaseResponse baseResponse = new BaseResponse<>();
        try {
            List<ApplicationFormRecordVo> recordVos = patientRecordQueryService.queryInspectionReportRecords(request);
            baseResponse.setData(recordVos);
        } catch (Exception e) {
            throw new CodedBaseRuntimeException(201, "查询检验、检查、治疗记录失败！");
        }
        return baseResponse;
    }

    @Override
    public BaseResponse surgicalRecords(ListQueryHisReq request) {
        logger.info("手术记录请求入参=" + JSONUtils.writeValueAsString(request));
        BaseResponse baseResponse = new BaseResponse<>();
        try {
            List<VrSurgicalRecord> recordVos = patientRecordQueryService.surgicalRecords(request);
            baseResponse.setData(recordVos);
        } catch (Exception e) {
            throw new CodedBaseRuntimeException(201, "查询手术记录失败！");
        }
        return baseResponse;
    }


}
